package com.lyl.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @author lylbell
 * @date 2021-06-17 19:15
 */
//web安全的配置
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable(); //关闭csrf
        http.authorizeRequests().anyRequest().authenticated(); //authorizeRequests:开启登录配置。anyRequest().authenticated()表示剩余的其他接口，登录之后就能访问
    }

    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }


    /**模拟数据库*/
/*    @Bean
    protected UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager memory = new InMemoryUserDetailsManager();
        User user = new User("admin","123456", Arrays.asList(new SimpleGrantedAuthority("Role_Admin")));
        memory.createUser(user);
        return memory;
    }*/

    /**
     * 注入密码的验证管理器
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
//        return NoOpPasswordEncoder.getInstance();
        return new BCryptPasswordEncoder();
    }
}
